package com.lantian.teacher.controller;

import com.lantian.common.result.Result;
import com.lantian.teacher.entity.EduSubject;
import com.lantian.teacher.entity.vo.OneSubject;
import com.lantian.teacher.service.EduSubjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @name:
 * @description:
 * @author: meet
 * @create: 2020-05-01 14:40
 */
@RestController
@RequestMapping("/subject")
@CrossOrigin
@Api(description = "课程科目管理")
public class EduSubjectCtrl {

    @Autowired
    private EduSubjectService subjectService;

    @ApiOperation(value = "读取03版Excel文件")
    @PostMapping("/import")
    public Result importSubject(
            @ApiParam(value = "Excel表格,获取1,2列")
            MultipartFile file){
        List<String> mesList = subjectService.importEXCL(file);
        // 因为考虑到EXCL模板中数据不准确所以返回多个错误信息，那么多个错误信息放在集合中
        if (mesList.size()==0){
            return Result.ok();
        }else {
            return Result.error().data("messageList", mesList);
        }
    }

    @ApiOperation(value = "获取分类数据")
    @GetMapping("/tree")
    public Result getTree(){
        List<OneSubject> subjectList= subjectService.getTree();
        return Result.ok().data("subjectList", subjectList);
    }

    @ApiOperation(value = "根据ID删除分列")
    @DeleteMapping("/{id}")
    public Result deleteById(
            @PathVariable String id) {
        boolean b = subjectService.deleteById(id);
        if(b){
            return Result.ok();
        } else{
            return  Result.error();
        }
    }

    @ApiOperation(value = "根据ID添加一级分类")
    @PostMapping("/saveLevelOne")
    public Result saveLevelOne(
            @ApiParam(name = "subject" , value = "课程分类对象", required = true)
            @RequestBody EduSubject eduSubject) {
        boolean b = subjectService.saveLevelOne(eduSubject);
        if(b){
            return Result.ok();
        } else{
            return  Result.error().message("添加失败");
        }
    }

    @ApiOperation(value = "根据ID添加二级分类对象")
    @PostMapping("/saveLevelTwo")
    public Result saveLevelTwo(
            @ApiParam(name = "subject" , value = "课程分类对象", required = true)
            @RequestBody EduSubject eduSubject) {
        boolean b = subjectService.saveLevelTwo(eduSubject);
        if(b){
            return Result.ok();
        } else{
            return  Result.error().message("添加失败");
        }
    }
}
